<template>
  <el-form
    ref="ruleForm"
    :model="form"
  >
    <el-form-item
      prop="email"
      :rules="{required:true,message:'请输入账号',trigger:'change'}"
    >
      <el-input
        v-model="form.email"
        placeholder="请输入账号"
        style="width: 280px;"
      />
    </el-form-item>
    <el-form-item
      prop="password"
      :rules="{required:true,message:'请输入密码',trigger:'change'}"
    >
      <el-input
        v-model="form.password"
        type="password"
        placeholder="请输入密码"
        style="width: 280px;"
      />
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        size="mini"
        @click="submitClick"
      >
        登录
      </el-button>
    </el-form-item>
  </el-form>
</template>
<script lang="ts">
import { defineComponent,reactive,ref,toRefs } from "vue"
import { useRouter } from "vue-router"
interface UserForm {
   email: string;
   password: string | number 
}
export default defineComponent({
  name: "DymicLogin",
  setup() {
    const router = useRouter() // 先定义路由
    const state = reactive({
      form:{
        email:"admin",
        password:"123456"
      } as UserForm,
      ruleForm:ref<any>(null) // ruleForm: ref<any>(null) 声明ruleForm,并返回
    })

    const submitClick = () => {
      state.ruleForm.validate(valid => { // state.ruleForm.validate() 而不是 state.ruleForm.value.validate()
        console.log("登录请求校验中....")
        if(valid) {
          if(state.form.email === "admin") {
            console.log("登录成功")
            router.push({ path:"/" })
          }
        }
      })
    }

    return {
      ...toRefs(state),
      submitClick
    }
  }
})
</script>
<style lang='less' scoped>

</style>
